import LevelBaseControl from "../LevelCommon/LevelBaseControl";
import RotateObj from "../Prefabs/InteractivObj/Common/RotateObj";
import ArtBoard from "../Prefabs/InteractivObj/ArtBoard";
import Gate from "../Prefabs/InteractivObj/Gate";
import EnumData from "../../Enum/EnumData";
import { utils } from "../../utils/CommonUtil";
import DialogDataConst from "../../constant/DialogDataConst";
import EventNameList from "../../constant/EventNameList";
import GamePropConst from "../../constant/GamePropConst";

export default class LevelControl4 extends LevelBaseControl{
    
    public  test2 :boolean= false;
    public  test1:boolean = false;
    /** 地窖是否打开了*/
    private isDiJiaoOpen:boolean = false;
    /** 各房间模块的索引*/
    private room1Index:number = 0;
    private room2Index:number = 1;
    //#region room1
    /** 密码锁的长柜*/
    private passDoorPanel:RotateObj;    
    /** 三幅画框*/
    private room1Arts:Array<ArtBoard> = new Array<ArtBoard>();
    /** 窗口的箱子*/
    private windowBox:Laya.Sprite3D;
    /** 地窖门*/
    private palletGate:RotateObj;
    /** 地窖锁*/
    private tie_lock_room1:Laya.Sprite3D;        
    /** 门*/
    private gateRoom1:Gate;
    //#endregion
    //#region room2
    /** 电子密码锁下面的柜子板*/
    private passLockPanel:RotateObj;
    /** 电子密码锁上的按键5*/
    private passLockKey5:Laya.Sprite3D;
    /** 电子密码锁上的按键8*/
    private passLockKey8:Laya.Sprite3D;
    /** 挡在门上的椅子是否已经移开了*/
    private isChairMoveOut:boolean = false;
    //#endregion
    protected startByBase(){
        this.InitRoom1();
        this.InitRoom2()
    }

    onUpdate()
    {
        if (this.test2)
        {
            this.test2 = false;
            this.ChangeCurLevelObj(this.allLevelObjs[this.room2Index], EnumData.CameraMoveEff.LeftToRight);
        }
        if (this.test1)
        {
            this.test1 = false;
            this.ChangeCurLevelObj(this.allLevelObjs[this.room1Index],EnumData.CameraMoveEff.RightToLeft);
        }
    }

    private InitRoom1()
    {
        let room1:Laya.Node = this.allLevelObjs[this.room1Index].owner;
        let Container = room1.getChildByName("Container");
        let Door_panel = Container.getChildByName("Door_panel");
        this.passDoorPanel = utils.CommonUtils.CustomAddComponent(Door_panel,RotateObj);

        let room1Walls = room1.getChildByName("walls");
        let room1Wall1 = room1Walls.getChildByName("wall1");
        let room1Wall2 = room1Walls.getChildByName("wall2");

        let paintings = room1Wall1.getChildByName("paintings");
        utils.CommonUtils.GetComponentsInChildren<ArtBoard>(paintings as Laya.Sprite3D,ArtBoard,this.room1Arts);
        this.windowBox = (room1Wall2.getChildByName("windowBoxes").getChildByName("box_props_on") as Laya.Sprite3D);
        this.gateRoom1 = utils.CommonUtils.CustomAddComponent(room1Wall2.getChildByName("door_props_room1"),Gate);
        
        this.tie_lock_room1 =  (Container.getChildByName("Pallet_props").getChildByName("checkFocus").getChildByName("tie_lock_room1")as Laya.Sprite3D);
        this.palletGate = Container.getChildByName("Pallet_props").getComponent(RotateObj);
    }

    private InitRoom2()
    {
        let room2:Laya.Node = this.allLevelObjs[this.room2Index].owner;
        let room2Walls = room2.getChildByName("walls");
        let passLockMachine:Laya.Node = room2Walls.getChildByName("wall2").getChildByName("Password machine");

        this.passLockPanel = passLockMachine.getChildByName("password_panel_props").getComponent(RotateObj);

        let passwordLockRoom2 = passLockMachine.getChildByName("passwordLockRoom2");

        this.passLockKey5 = (passwordLockRoom2.getChildByName("keyBoard").getChildByName("5") as Laya.Sprite3D); 

        this.passLockKey8 = (passwordLockRoom2.getChildByName("keyBoard").getChildByName("8") as Laya.Sprite3D); 
    }

    public  DoInteractObjLogic( _name:string):boolean
    {
        switch (_name)
        {
            /** room1     *********************************************************************************************/
            /** 密码锁开了*/
            case "physPassLock_UnLock":
                this.passDoorPanel.Interactive();

                break;
            /** 检测三幅画是不是都装上了*/
            case "ArtBoard":
            
                for (let index = 0; index < this.room1Arts.length; index++) {
                    let item = this.room1Arts[index];
                    if (!item.IsHavePainting())
                    {
                        return false;
                    }
                }
                
                /** 画都装上了*/
                this.ShowDialog(DialogDataConst.fallen_obj);
                /** 注册箱子落下事件 在相机归位后执行*/
                window.gameFacade.EventMng.add(EventNameList.CameraResetPosEvent,this,this.PutDownWindowBox)
                break;
                /** 窗口的箱子 如果之前的箱子落下来了 就能爬上去*/
            case "windowBoxes":
            case "box_props_on":
                if (this.windowBox.active)
                {
                    /** 到第二个房间*/
                    if (this.cameraControl.curRoateY == 0)
                    {
                        this.ChangeCurLevelObj(this.allLevelObjs[this.room2Index], EnumData.CameraMoveEff.LeftToRight);
                    }
                    else
                    {
                        this.ChangeCurLevelObj(this.allLevelObjs[this.room2Index], EnumData.CameraMoveEff.RightToLeft);
                    }
                }
                else
                {
                    this.ShowDialog(DialogDataConst.cant_climp_up);
                }
                break;
                /** 点击门 如果第二个房间门上的椅子没移开 就不准操作*/
            case "door_props_room1":
                return this.isChairMoveOut;
                /** 进入room1的门  到room2*/
            case "door_props_room1_In_Gate":
                /** 到第二个房间*/
                if (this.cameraControl.curRoateY == 0)
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[this.room2Index], EnumData.CameraMoveEff.LeftToRight);
                }
                else
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[this.room2Index], EnumData.CameraMoveEff.RightToLeft);
                }
                break;

            /** 点击地窖的锁*/
            case "tie_lock_room1":
                /** 选择钥匙 就打开地窖*/
                if (this.IsSelectProp(GamePropConst.ID_KEY_GREEN))
                {
                    /** 使用道具*/
                    this.UseCurProp();
                    /** 销毁锁*/
                    this.tie_lock_room1.destroy();
                    /** 打开地窖门*/
                    this.palletGate.Interactive();
                    this.isDiJiaoOpen = true;
                }
                else
                {
                    this.ShowDialog(DialogDataConst.locked);
                }
                break;
            /** 点击地窖梯子 过关*/
            case "Ladder":
                if(this.isDiJiaoOpen)
                {
                    this.FinishLevel();
                }
                break;
            /** round2    *********************************************************************************************/
            /** 坏掉的按键5*/
            case "Broken button_05":                
                if (!this.passLockKey5.active)
                {
                    /** 如果选中道具按钮5就放上去*/
                    if (this.IsSelectProp(GamePropConst.ID_BUTTON_5))
                    {
                        this.UseCurProp();
                        this.passLockKey5.active = true;
                    }
                }
                break;
            /** 坏掉的按键8*/
            case "Broken button_08":
                if (!this.passLockKey8.active)
                {
                    /** 如果选中道具按钮8就放上去*/
                    if (this.IsSelectProp(GamePropConst.ID_BUTTON_8))
                    {
                        this.UseCurProp();
                        this.passLockKey8.active = true;
                    }
                }
                break;
            /** 电子密码锁打开了*/
            case "passwordLockRoom2":
                /** 底下柜子打开*/
                this.passLockPanel.Interactive();
                break;
            /** 点击挡在门上的椅子  如果已经移开了就不管 没有就返回true 移开*/
            case "chair_props_room2":
                if (this.isChairMoveOut)
                {
                    return false;
                }
                else
                {                    
                    this.isChairMoveOut = true;
                }                
                break;
            /** 点击到room2的门 如果椅子没移开 就 返回false 不操作 不然就返回true*/
            case "door_props_room2":
                return this.isChairMoveOut;
                break;
                /** */
            /** 打开了room2的门 同时要打开room1的门*/
            case "door_props_room2_Open_Gate":
                this.gateRoom1.SetOpenDirect();
                break;
            /** 从room2进入room1*/
            case "door_props_room2_In_Gate":
                if (this.cameraControl.curRoateY == -90)
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[this.room1Index],EnumData.CameraMoveEff.RightToLeft);
                }
                else
                {
                    this.ChangeCurLevelObj(this.allLevelObjs[this.room1Index], EnumData.CameraMoveEff.LeftToRight);
                }
                break;
            default:
                break;
        }
        return true;
    }

    /** room1房间窗口的箱子落下*/
    private PutDownWindowBox( obj:any)
    {
        Laya.timer.once(100,this,this.ActiveWindowBox);
        window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent,this,this.PutDownWindowBox);
    }

    /** 显示出来要下落的箱子并落下*/
    private ActiveWindowBox()
    {
        this.windowBox.active = true;
    }

    onDestroy()
    {
        window.gameFacade.EventMng.remove(EventNameList.CameraResetPosEvent,this,this.PutDownWindowBox);
    }
}